package com.turtle3d.formallanguage;

import java.util.ArrayList;
import java.util.Random;

import com.turtle3d.Randomizer;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.turtle.Turtle3D;
import com.turtle3d.visualizator.VisualData;



public class TurtleController {
	private Turtle3D turtle3D;
	
	private VisualData visualData = new VisualData();
	
	private Random random = new Random();
	//private ArrayList<VisualSymbol> visualSequence = new ArrayList<VisualSymbol>();
	
	private float angle = 30;

	public TurtleController(Turtle3D turtle3D) {
		super();
		this.turtle3D = turtle3D;
	}
	
	public void interpretLSymbol(Symbol symbol){												
		
		VisualSymbol visualSymbol = new VisualSymbol(symbol, turtle3D.getTransformMatrix(), turtle3D.getDepth());
		visualData.getVisualSequence().add(visualSymbol);
		
		String s = symbol.getSymbol(); 
		if(s.equals("F")){
			turtle3D.forward(symbol.getParameter("distance"));					
		}
		else if(s.equals("+")){
			turtle3D.turn(-symbol.getParameter("turnAngle"));
		}
		else if(s.equals("-")){
			turtle3D.turn(symbol.getParameter("turnAngle"));
		}
		else if(s.equals("[")){
			turtle3D.push();			
		}
		else if(s.equals("]")){
			turtle3D.pop();
		}
		else if(s.equals("<")){
			turtle3D.roll(-90);
		}
		else if(s.equals(">")){
			turtle3D.roll(90);
		}
		else if(s.equals("R")){			
			turtle3D.transform(Randomizer.getRandomizer().nextConeAngleTransformation(symbol.getParameter("spreadVariation"), true));
			//turtle3D.roll((float)random.nextFloat()*360f);
			//turtle3D.turn((float)random.nextFloat()*40f-20f);			
		}
		
	}
	
	public void interpretLSymbols(ArrayList<Symbol> symbols){
		for(Symbol s:symbols){
			interpretLSymbol(s);
		}
		
		visualData.getVisualSequence().add(new VisualSymbol(symbols.get(symbols.size()-1), turtle3D.getTransformMatrix()));
		visualData.setMaximalDepthReached(turtle3D.getMaximalDepth());
	}


	public float getAngle()
	{
		return angle;
	}

	public void setAngle(float angle)
	{
		this.angle = angle;
	}

	public VisualData getVisualData()
	{
		return visualData;
	}
}
